#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


#define idioma_ing 0
#define idioma_esp 1
#define idioma_rand 2
#define size_bloque 4096

char *path="/home/tesso/contextoconfuso.txt";



char* ingles[]={"the", "of", "and", "to", "in","a" ,"is" ,"that" ,"be", "it", "by", "are", "for", "was", "as", "he", "with", "on", "his", "at", "which",
			"but", "from", "has", "this", "will", "one", "have", "not", "were" ,"or", "all" ,"their" ,"an" ,"i" ,"there", "been" ,"many", "more",
			"so", "when", "had", "may", "today", "who", "would" ,"time" ,"we", "about" ,"after", "dollars", "if" ,"my", "other", "some",
			"them", "being", "its", "no" ,"only" ,"over" ,"very", "you" ,"into" ,"most" ,"than" ,"they", "day" ,"even", "made", "out", "first",
			"great", "must", "these", "can", "days", "every" ,"found", "general", "her", "here", "last", "new", "now", "people",
			"public" ,"said", "since" ,"still" ,"such" ,"through", "under", "up" ,"war", "well" ,"where", "while", "years", "before",
			"between", "country", "debts", "good" ,"him" ,"interest", "large" ,"like" ,"make", "our", "take", "upon" ,"what"};


char *espaniol[]={"acerca" ,"agradable", "algunos", "alimiento", "aprender" ,"aun" ,"ayudar" ,"bueno" ,"caliente",
			"cerca", "comer", "como", "con", "condicional" ,"conocer", "conseguir", "cuando", "dar",
			"desde", "despues" ,"disfrutar" ,"diversion" ,"donde" ,"dormir" ,"echar" ,"el" ,"encontrar",
			"encontrarse", "gustar", "hacer", "hacer", "hola", "intentar", "joder" ,"jugar" ,"las",
			"llegar" ,"los", "menos", "mirar", "mostrar", "nosotros", "pensar", "poder" ,"porque",
			"proximo", "que", "quedarse", "querer" ,"ser", "solo" ,"tardar" ,"tener", "tiene",
			"todavia", "todos", "trabajar" ,"una", "venir", "ver" ,"voluntad", "el","las","ellos","de","para", "que","con","y","por"};



void vaciar_string(char string[]){
	int i=0;
	while(i<strlen(string)){
		string[i]='0';
		i+=1;
	}
}


//Si se encuentra el 20% de las palabras de un idioma en la lista, y si el porcentaje de ese idioma es 4 veces mayor al otro idioma, entonces se selecciona ese idioma. Sino es random
int reconocimiento_idioma(char array[]){
		int i=0;
		char aux[15];
		int j=0,indice_esp=0,indice_ing=0,estado;
		bool encontrado_esp,encontrado_ing;
		int es_espaniol=0,es_ingles=0,es_random=0,no_habitual=0;
		int cantidad_De_palabras=0;
		while (i<strlen(array)){
			if (array[i]!=' ' && array[i]!='.' && array[i]!=',' ){
				if(j==15){
					es_random+=1;
					j=0;
					vaciar_string(aux);
				}
				else{
					aux[j]=array[i];
					j+=1;
				}
			}
			else{
				if(j!=0){
					cantidad_De_palabras+=1;
				encontrado_esp=false;
				encontrado_ing=false;
				indice_esp=0;
				while(indice_esp<70 && !encontrado_esp){
					if(j==strlen(espaniol[indice_esp])){
						estado=strncmp(aux,espaniol[indice_esp],j);
					}
					else{
						estado=1;
					}
					if(estado==0){

						es_espaniol+=1;
						encontrado_esp=true;
					}
					indice_esp+=1;
				}

				if(!encontrado_esp){
					indice_ing=0;
					while(indice_ing<96 && !encontrado_ing){
							if(j==strlen(ingles[indice_ing])){
								estado=strncmp(aux,ingles[indice_ing],j);
							}
							else{
								estado=1;
							}
							if(estado==0){
								es_ingles+=1;
								encontrado_ing=true;
							}
							indice_ing+=1;

					}
				}
				if(!encontrado_ing && !encontrado_esp){
					no_habitual+=1;
				}
				j=0;
				vaciar_string(aux);
				}
			}
			i+=1;
		}
		float porcentaje_esp,porcentaje_ing,porcentaje_rand,porcentaje_no_h;
		porcentaje_esp=((float)es_espaniol/(float)cantidad_De_palabras)*100;
		porcentaje_ing=((float)es_ingles/(float)cantidad_De_palabras)*100.;
		porcentaje_rand=((float)es_random/(float)cantidad_De_palabras)*100;
		porcentaje_no_h=((float)no_habitual/(float)cantidad_De_palabras)*100;
		if (porcentaje_esp>4*porcentaje_ing && porcentaje_esp>15.0 && porcentaje_rand<10.0){
				return idioma_esp;
		}
		else if (porcentaje_ing>4*porcentaje_esp && porcentaje_ing>15.0 && porcentaje_rand<10.0){
				return idioma_ing;
		}
		else{
			return idioma_rand;
		}

}

/*
int main(){
	int fd=open(path,0);
	char buff[size_bloque];
	int buf_size=size_bloque;
	if(read( fd , buff , buf_size ) > 0 ){
		reconocimiento_idioma(buff);
	}
	close(fd);
	return 0;
}*/
